<?php
namespace app\platform\controller\admin;
use app\BaseController;
use app\common\logic\DataLogic;
use app\common\logic\TokenLogic;
use app\common\model\account\AccountModel;
use app\common\model\account\ShopAccountModel;
use app\common\model\TokenModel;
use think\facade\Db;

class Account extends BaseController{

    public function index()
    {
        $status=$this->request->param('status/d');
        $keyword=$this->request->param('keyword');
        $username=$this->request->param('username');
        $mobile=$this->request->param('mobile');
        $where=[
            ['is_delete','=',0]
        ];

        if(!empty($username)){
            $where[]=['username','=',$username];
        }
        if(!empty($mobile)){
            $where[]=['mobile','=',$mobile];
        }
        if(!empty($status)){
            $where[]=['status','=',$status];
        }
        if(!empty($keyword)){
            $where[]=['mobile|username','like','%'.$keyword.'%'];
        }
        $account=new AccountModel();
        $account = $account->withCount(['createShop'=>function($query){
            $query->where([['role_id','=',1]]);
        },'manageShop']);
        $order=['add_time'=>'desc'];
        $append=['add_time_format','last_login_time_format'];
        $data=DataLogic::getDataList($account,$where,$order,'*',$this->request->limit,$this->request->page,$append);
        return $this->success($data);
    }

    public function addAccount()
    {
        $username=$this->request->param('username');
        $mobile=$this->request->param('mobile');
        $password=$this->request->param('password');

        if(empty($username)) return $this->error('请输入账号名称');
        if(empty($mobile) || !is_mobile($mobile)) return $this->error('请输入手机号');
        if(empty($password)) return $this->error('请输入密码');

        if(null !== AccountModel::where([
            ['mobile','=',$mobile],
            ['is_delete','=',0],
        ])->find()){
            return $this->error('手机号已存在，请重新输入手机号');
        }

        $account_model=new AccountModel();
        $account_model->username=$username;
        $account_model->mobile=$mobile;
        $account_model->password=md5($password);
        $account_model->code=rand(111111,999999);
        $account_model->save();

        return $this->success('添加成功');
    }

    public function editAccount()
    {

        $username=$this->request->param('username');
        $mobile=$this->request->param('mobile');
        $password=$this->request->param('password');
        $account_id=$this->request->param('account_id');

        if(null !== AccountModel::where([
                ['mobile','=',$mobile],
                ['is_delete','=',0],
            ])->find()){
            return $this->error('手机号已存在，请重新输入手机号');
        }
        $AccountData = AccountModel::where([
            ['account_id','=',$account_id],
        ])->find();
        if($username == ''){
            $username = $AccountData['username'];
        }
        if($mobile == ''){
            $mobile = $AccountData['mobile'];
        }
        if($password == ''){
            $password = $AccountData['password'];
        } else {
            $password = md5($password);
        }

//        $account_model=new AccountModel();
        $AccountData->account_id=$account_id;
        $AccountData->username=$username;
        $AccountData->mobile=$mobile;
        $AccountData->password=$password;
        $AccountData->save();

        return $this->success('添加成功');
    }

    public function getAccountShopList()
    {
        $account_id=$this->request->param('account_id/d',0);
        if($account_id==0) return $this->error('账号不存在');
        if(null === $account = AccountModel::find($account_id)) return $this->error('账号不存在');
        if($account->status != 1 ) return $this->error('账号异常');
        if($account->is_delete != 0) return $this->error('账号不存在');

        $datas=ShopAccountModel::with(['shop'=>function($query){
            $query->with(['app'])->append(['expire_time_format']);
        }])->where([
            ['account_id','=',$account_id]
        ])->select()->append(['shop_token'])->toArray();
        
        return $this->success(['datas'=>$datas]);
    }
}
